package com.marketapp.controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javassist.SerialVersionUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.marketapp.domain.UserDetails;
import com.marketapp.domain.UserInfo;
import com.marketapp.helper.ServiceException;
import com.marketapp.service.IPasswordGenerator;
import com.marketapp.service.IUserService;
import com.marketapp.validators.UserInfoValidator;

@Controller
@RequestMapping(value = "/register")
public class RegistrationController {
	
	

	@Autowired
	IUserService userService;

	@Autowired
	UserInfoValidator validator;

	@Autowired
	IPasswordGenerator passwordGenerator;

	public UserInfoValidator getValidator() {
		return validator;
	}

	public void setValidator(UserInfoValidator validator) {
		this.validator = validator;
	}

	public IPasswordGenerator getPasswordGenerator() {
		return passwordGenerator;
	}

	public void setPasswordGenerator(IPasswordGenerator passwordGenerator) {
		this.passwordGenerator = passwordGenerator;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	@RequestMapping(method = RequestMethod.GET)
	public void setUpForm(Model model) {
		UserInfo userInfo = new UserInfo();
		UserDetails userDetails = new UserDetails();
		userInfo.setUserDetails(userDetails);
		model.addAttribute("user", userInfo);
	}

	@RequestMapping(method = RequestMethod.POST)
	public String submitForm(@ModelAttribute(value = "user") UserInfo userInfo,
			BindingResult result, Model model) throws ServiceException {
		validator.validate(userInfo, result);
		if (result.hasErrors()) {
			return "register";
		}
		userInfo.setUserPassword(passwordGenerator.generatePassword(userInfo
				.getUserPassword()));
		userService.createUser(userInfo);
		return "registration_success";
	}

/*	@RequestMapping(value = "/channel", method = RequestMethod.GET)
	public @ResponseBody
	Set<String> findAllChannel() {
		Set<String> channelSet = new HashSet<String>();
		channelSet.add("FAQ");
		channelSet.add("KNW");
		return channelSet;
	}

	@RequestMapping(value = "/categories", method = RequestMethod.GET)
	public @ResponseBody
	Set<String> categoriesForChannel(
			@RequestParam(value = "channelName", required = true) String category) {
		Set<String> categorySet = new HashSet<String>();
		categorySet.add("TECH");
		categorySet.add("DOM");
		return categorySet;
	}*/
}
